using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Search;

namespace PartyBoy.TweetMode.Components
{
    public class TweetModeOptionController : ISearchable, IPortable
    {
        #region public method
        /// <summary>
        /// Get an info object from the database
        /// </summary>
        /// <param name="moduleId"></param>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public TweetModeOptionInfo GetTweetModeOptionByModuleID(int moduleId)
        {
            return (TweetModeOptionInfo)CBO.FillObject(DataProvider.Instance().GetTweetModeOptionByModuleID(moduleId), typeof(TweetModeOptionInfo));
        }
        
        #endregion

        #region ISearchable Members

        /// <summary>
        /// Implements the search interface required to allow DNN to index/search the content of your
        /// module
        /// </summary>
        /// <param name="modInfo"></param>
        /// <returns></returns>
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo modInfo)
        {
            SearchItemInfoCollection searchItems = new SearchItemInfoCollection();

            //List<TweetModeOptionInfo> infos = GetTweetModeOptionByModuleID(modInfo.ModuleID);

            //foreach (TweetModeOptionInfo info in infos)
            //{
            //    SearchItemInfo searchInfo = new SearchItemInfo(modInfo.ModuleTitle, info.Content, info.CreatedByUser, info.CreatedDate,
            //                                        modInfo.ModuleID, info.ItemId.ToString(), info.Content, "Item=" + info.ItemId.ToString());
            //    searchItems.Add(searchInfo);
            //}

            return searchItems;
        }

        #endregion

        #region IPortable Members

        /// <summary>
        /// Exports a module to xml
        /// </summary>
        /// <param name="ModuleID"></param>
        /// <returns></returns>
        public string ExportModule(int moduleID)
        {
            StringBuilder sb = new StringBuilder();

            //List<TweetModeOptionInfo> infos = Getsamples(moduleID);

            //if (infos.Count > 0)
            //{
            //    sb.Append("<samples>");
            //    foreach (TweetModeOptionInfo info in infos)
            //    {
            //        sb.Append("<sample>");
            //        sb.Append("<content>");
            //        sb.Append(XmlUtils.XMLEncode(info.Content));
            //        sb.Append("</content>");
            //        sb.Append("</sample>");
            //    }
            //    sb.Append("</samples>");
            //}

            return sb.ToString();
        }

        /// <summary>
        /// imports a module from an xml file
        /// </summary>
        /// <param name="ModuleID"></param>
        /// <param name="Content"></param>
        /// <param name="Version"></param>
        /// <param name="UserID"></param>
        public void ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            //XmlNode infos = DotNetNuke.Common.Globals.GetContent(Content, "samples");

            //foreach (XmlNode info in infos.SelectNodes("sample"))
            //{
            //    sampleInfo sampleInfo = new sampleInfo();
            //    sampleInfo.ModuleId = ModuleID;
            //    sampleInfo.Content = info.SelectSingleNode("content").InnerText;
            //    sampleInfo.CreatedByUser = UserID;

            //    Addsample(sampleInfo);
            //}
        }

        #endregion
    }
}
